package com.shixi.service;

import com.github.pagehelper.PageHelper;
import com.shixi.dao.ProductDao;
import com.shixi.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {

    @Autowired
    ProductDao productDao;

    /*获取所有商品*/
    public List<Product> getAllProduct(int page, int limit) {
        PageHelper.startPage(page, limit);
        return productDao.getAllProduct();
    }

    /*根据用户名查询商品*/
    public List<Product> getUserProduct(int page, int limit, String username) {
        PageHelper.startPage(page, limit);
        return productDao.getUserProduct(username);
    }

    /*关键字查询商品*/
    public List<Product> getMatchingProduct(String matchString) {
        return productDao.getMatchingProduct(matchString);
    }
    public List<Product> getMatchingUserProduct(String username, String matchString){
        return productDao.getMatchingUserProduct(username,matchString);
    }

    /*根据id查询商品*/
    public Product getProductById(int id) {
        return productDao.getProductById(id);
    }

    /*更新商品信息*/
    public boolean updateProductInfo(int goods_id, String goods_name, String goods_des, int goods_price, int goods_count) {
        if (productDao.updateProductInfo(goods_id, goods_name, goods_des, goods_count, goods_price) > 0
            && productDao.updareProductInfoRecord(goods_id, goods_name, goods_des, goods_price) > 0) {
            return true;
        } else {
            return false;
        }
    }

    /*删除商品*/
    public boolean deleteProductById(int goods_id) {
        if (productDao.deleteProductById(goods_id) > 0) {

            return true;
        } else {
            return false;
        }
    }

    /*添加商品*/
    public boolean insertProduct(String goods_username, String goods_name, String goods_des, String goods_count, String goods_price, String goods_picurl){
        System.out.println(goods_username+goods_name+goods_des+goods_count+goods_price+goods_picurl);
        if(productDao.insertProduct(goods_username, goods_name, goods_des, goods_count, goods_price, goods_picurl) > 0){
            Product product = productDao.getProductByUrl(goods_picurl);
            System.out.println(product.getGoods_picurl()+"******************");
            productDao.insertProductRecord(Integer.parseInt(product.getGoods_id()),product.getGoods_username(),product.getGoods_name(),product.getGoods_des(),Integer.parseInt(product.getGoods_price()));
            return true;
        }else{
            return false;
        }
    }
}
